/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */




import java.io.IOException;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author JSJSYS
 */
@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
     protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws SQLException, ClassNotFoundException, ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            String name = request.getParameter("name");
            String password = request.getParameter("pwd");
            String sql = "select  *  from STU_INFO where name=? and password =?";
            DbUtils db = new DbUtils();
            db.getConnection();
            List param = new ArrayList();
            param.add(name);
            param.add(password);
        try {
            List<Map<String, Object>> result = db.findResult(sql, param);
            if (result.size() == 1) {
                request.setAttribute("u", result.get(0));
                // 
                sql = "select * from STU_INFO limit 10";
                result = db.findResult(sql, new ArrayList());
                request.setAttribute("list", result);
                request.getRequestDispatcher("jsp/success.jsp").forward(request, response);
            } else {
                request.setAttribute("msg", "error input!!!");
                sql = "update STU_INFO set password=? where name=? ";
                param = new ArrayList();
                param.add("000");
                param.add(name);
                db.updateByPreparedStatement(sql, param);
                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Mylogin.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            db.releaseConn();
        }
        //request.setAttribute("msg", "error input!!!");
        request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
    }
@Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }
    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

//            userdao test = new userdao();
//
//            ResultSet rs;
//
//            test.initDB();
//
//            rs=test.selectalluser();
//
//            String name="",psw="";
//
//            String tmpn=request.getParameter("userName"),tmpp=request.getParameter("password");
//
//            while(rs.next())
//
//            {
//
//                name=rs.getString(1);
//
//                if(name.equals(tmpn))
//
//                {   
//
//                    psw=rs.getString(2);
//
//                    break;
//
//                }
//
//            }
//
//            if(name.equals(tmpn)&&psw.equals(tmpp))
//
//            {
//
//                request.getRequestDispatcher("jsp/ok.jsp").forward(request, response);
//
//            }
//
//            else if(psw=="") 
//
//            {
//
//                request.setAttribute("msg", "用户名不存在");
//
//                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
//
//            }
//
//            else
//
//            {
//
//                request.setAttribute("userName", name);
//
//                request.setAttribute("msg", "密码错误");
//
//                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
//
//            }
   }
}


    

